package com.randao.framework.common.api;

import com.github.pagehelper.PageInfo;
import lombok.Data;
import org.springframework.data.domain.Page;

import java.util.List;

/**
 * 分页数据封装类
 *
 * @Author： ~Mr·Weng~
 * @CreateTime： 2019年-05月-01日  0:12
 * Look at the wine in the glass toast the rich first.
 */
@Data
public class CommonPage<T> {
    private Integer pageNum;    //第几页
    private Integer pageSize;   //每页条数
    private Integer totalPage;  //总页数
    private Long total; //总记录数（总条数）
    private List<T> list;   //数据集合


    /**
     * 将PageHelper分页后的list转为分页信息
     *
     * @param list
     * @param <T>
     * @return
     */
    public static <T> CommonPage<T> restPage(List<T> list) {
        //创建创建CommonPage对象对象
        CommonPage<T> result = new CommonPage<T>();
        //创建pageInfo对象，把用户传过来的list设置进去
        PageInfo<T> pageInfo = new PageInfo<>(list);
        //设置属性
        result.setPageNum(pageInfo.getPageNum());
        result.setPageSize(pageInfo.getPageSize());
        result.setTotalPage(pageInfo.getPages());
        result.setTotal(pageInfo.getTotal());
        result.setList(pageInfo.getList());
        //返回CommonPage对象的结果
        return result;
    }


    /**
     * 将SpringData分页后的list转为分页信息
     *
     * @param pageInfo
     * @param <T>
     * @return
     */
    public static <T> CommonPage<T> restPage(Page<T> pageInfo) {
        CommonPage<T> result = new CommonPage<T>();
        result.setTotalPage(pageInfo.getTotalPages());
        result.setPageNum(pageInfo.getNumber());
        result.setPageSize(pageInfo.getSize());
        result.setTotal(pageInfo.getTotalElements());
        result.setList(pageInfo.getContent());
        return result;
    }


}
